package test.test_7_2;

/*
 定义一个接口Compute，用来完成计算器的功能，比如最简单的加减乘除功能。
 请用以下两种方式测试：
   1，编写实现类进行测试
   2，用局部内部类进行测试
   3，使用匿名内部类进行测试
 */
public class Demo01 {
    public static void main(String[] args) {
        //1.编写实现类进行测试
        Test test = new Test();
        System.out.println(test.add(1, 2));//加
        System.out.println(test.subtract(4, 2));//减
        System.out.println(test.multiply(2, 2));//乘
        System.out.println(test.divide(3, 2));//除
        System.out.println("***********************");

        //2.编写局部内部类进行测试
        class Method_Inner implements Compute {

            @Override
            public int add(int a, int b) {
                return a + b;
            }

            @Override
            public int subtract(int a, int b) {
                return a - b;
            }

            @Override
            public int multiply(int a, int b) {
                return a * b;
            }

            @Override
            public double divide(int a, int b) {
                return 1.0 * a / b;
            }
        }
        Method_Inner m = new Method_Inner();
        System.out.println(m.add(1, 2));
        System.out.println(m.subtract(4, 2));
        System.out.println(m.multiply(2, 2));
        System.out.println(m.divide(3, 2));
        System.out.println("***********************");

        //使用成员内部类测试
        Test_Inner test_inner = new Test_Inner();
        System.out.println(test_inner.add(1, 2));
        System.out.println(test_inner.subtract(4, 2));
        System.out.println(test_inner.multiply(2, 2));
        System.out.println(test_inner.divide(3, 2));
        System.out.println("***********************");

        //3.用匿名内部类进行测试
        Compute compute = new Compute() {
            @Override
            public int add(int a, int b) {
                return a + b;
            }

            @Override
            public int subtract(int a, int b) {
                return a - b;
            }

            @Override
            public int multiply(int a, int b) {
                return a * b;
            }

            @Override
            public double divide(int a, int b) {
                return 1.0 * a / b;
            }
        };
        System.out.println(compute.add(1, 2));
        System.out.println(compute.subtract(4, 2));
        System.out.println(compute.multiply(2, 2));
        System.out.println(compute.divide(3, 2));

    }

    static class Test_Inner implements Compute {
        @Override
        public int add(int a, int b) {
            return a + b;
        }

        @Override
        public int subtract(int a, int b) {
            return a - b;
        }

        @Override
        public int multiply(int a, int b) {
            return a * b;
        }

        @Override
        public double divide(int a, int b) {
            return 1.0 * a / b;
        }
        //2，用局部内部类进行测试


    }

}

interface Compute {
    public abstract int add(int a, int b);//加

    public abstract int subtract(int a, int b);//减

    public abstract int multiply(int a, int b);//乘

    public abstract double divide(int a, int b);//除


}

class Test implements Compute {

    @Override
    public int add(int a, int b) {
        return a + b;
    }

    @Override
    public int subtract(int a, int b) {
        return a - b;
    }

    @Override
    public int multiply(int a, int b) {
        return a * b;
    }

    @Override
    public double divide(int a, int b) {
        return 1.0 * a / b;
    }

    public Test() {
    }


}
